package com.wutianyi.module.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Text;
import com.wutianyi.module.commons.PropertyGroup;
import com.wutianyi.module.company.Company;
import com.wutianyi.module.dao.CompanyDao;
import com.wutianyi.utils.Utils;

import static com.wutianyi.entity.EntityTypeEnum.Company;
import static com.wutianyi.entity.DataStoreField.DS_P_VALUE;
import static com.wutianyi.entity.DataStoreField.DS_P_CSID;

/**
 * @author hanjiewu
 * 
 */
public class CompanyDaoDataStoreImpl implements CompanyDao {
	
	private static DatastoreService datastoreService = DatastoreServiceFactory
			.getDatastoreService();
	
	private void setEntity(Company company) {
		int id = 0 != company.getId() ? company.getId() : company.hashCode();
		company.setGmtModified(new Date());
		Entity entity = new Entity(Company.name(), id);
		entity.setProperty(DS_P_VALUE, new Text(Utils.serialize(company)));
		entity.setProperty(DS_P_CSID, company.getCompanyId());
		datastoreService.put(entity);
		company.setId(id);
	}
	
	@Override
	public void insertCompany(Company company) {
		company.setId(0);
		setEntity(company);
	}
	
	@Override
	public void updateCompany(Company company) {
		setEntity(company);
	}
	
	@Override
	public Company getCompanyByCId(String companyId) {
		Query query = new Query(Company.name());
		query.addFilter(DS_P_CSID, FilterOperator.EQUAL, companyId);
		Entity entity = datastoreService.prepare(query).asSingleEntity();
		
		if (null != entity) {
			Company company = (Company) Utils.unSerialize(entity.getProperty(DS_P_VALUE));
			company.setId((int) entity.getKey().getId());
			return company;
		}
		
		return null;
	}
	
	@Override
	public int deleteCompanyByCId(String companyId) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	@Override
	public List<PropertyGroup> getCompanyStandarPropertyGroups() {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public Company getCompanyBaseInfoByCId(String companyId) {
		return getCompanyByCId(companyId);
	}
	
	@Override
	public List<Company> listCompanys() {
		Query query = new Query(Company.name());
		Iterable<Entity> iters = datastoreService.prepare(query).asIterable();
		List<Company> companys = new ArrayList<Company>();
		
		for (Entity entity : iters) {
			Company company = (Company) Utils.unSerialize(entity.getProperty(DS_P_VALUE));
			company.setId((int) entity.getKey().getId());
			companys.add(company);
		}
		
		return companys;
	}
	
	@Override
	public Company getCompanyById(int id) {
		Query query = new Query(Company.name(), KeyFactory.createKey(Company.name(), id));
		Entity entity = datastoreService.prepare(query).asSingleEntity();
		
		if (null != entity) {
			Company company = (Company) Utils.unSerialize(entity.getProperty(DS_P_VALUE));
			company.setId((int) entity.getKey().getId());
			return company;
		}
		
		return null;
	}
	
}
